/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Asign.java
 *
 * Created on 03/05/2011, 10:34:10
 */
package creator;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JList;
import utn.tsb.tej.world_logic.CardType;
import utn.tsb.tej.world_logic.Color;
import utn.tsb.tej.world_logic.Continent;
import utn.tsb.tej.world_logic.Country;
import utn.tsb.tej.world_logic.World;
import csm.uttils.Archive;
import csm.uttils.generics.Roster;

/**
 *
 * @author Administrator
 */
public class Asign extends javax.swing.JFrame {

    /** Creates new form Asign */
    public Asign() {
        initComponents();
        rstCountries = new Roster<Country>();
        lstUnasigned.setModel(lstmUnasigned);
        lstLandBordering.setModel(lstmLand);
        lstSeaBordering.setModel(lstmSea);
        cbxCountries.setModel(cbxmCountries);
        btnAddLand.setMnemonic(76);
        btnAddSea.setMnemonic(83);
        btnSave.setMnemonic(71);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblName = new javax.swing.JLabel();
        lblCodeRgb = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblX = new javax.swing.JLabel();
        lblY = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtCodeRGB = new javax.swing.JTextField();
        txtX = new javax.swing.JTextField();
        txtY = new javax.swing.JTextField();
        cbxJoker = new javax.swing.JCheckBox();
        cbxAir = new javax.swing.JCheckBox();
        cbxWater = new javax.swing.JCheckBox();
        cbxEarth = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnAddSea = new javax.swing.JButton();
        btnAddLand = new javax.swing.JButton();
        btnSubLand = new javax.swing.JButton();
        btnSubSea = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnLoad = new javax.swing.JButton();
        scpCountries = new javax.swing.JScrollPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        lstUnasigned = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstLandBordering = new javax.swing.JList();
        jScrollPane5 = new javax.swing.JScrollPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstSeaBordering = new javax.swing.JList();
        cbxCountries = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblName.setText("Nombre");

        lblCodeRgb.setText("CodeRGB");

        jLabel4.setText("Types");

        lblX.setText("X");

        lblY.setText("Y");

        txtName.setEditable(false);

        txtCodeRGB.setEditable(false);

        txtX.setEditable(false);

        txtY.setEditable(false);

        cbxJoker.setText("JOKER");
        cbxJoker.setEnabled(false);

        cbxAir.setText("AIR");
        cbxAir.setEnabled(false);

        cbxWater.setText("WATER");
        cbxWater.setEnabled(false);

        cbxEarth.setText("EARTH");
        cbxEarth.setEnabled(false);

        jLabel1.setText("Country");

        jLabel3.setText("LandBordering");

        jLabel5.setText("SeaBordering");

        btnAddSea.setText("Sea>>");
        btnAddSea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddSeaActionPerformed(evt);
            }
        });

        btnAddLand.setText("Land>>");
        btnAddLand.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddLandActionPerformed(evt);
            }
        });

        btnSubLand.setText("<<");
        btnSubLand.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubLandActionPerformed(evt);
            }
        });

        btnSubSea.setText("<<");
        btnSubSea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubSeaActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnLoad.setText("Load");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        lstUnasigned.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstUnasignedValueChanged(evt);
            }
        });
        jScrollPane4.setViewportView(lstUnasigned);

        scpCountries.setViewportView(jScrollPane4);

        jScrollPane2.setViewportView(lstLandBordering);

        jScrollPane1.setViewportView(jScrollPane2);

        jScrollPane3.setViewportView(lstSeaBordering);

        jScrollPane5.setViewportView(jScrollPane3);

        cbxCountries.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxCountriesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addComponent(cbxAir))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addComponent(cbxJoker))
                            .addComponent(cbxWater)
                            .addComponent(cbxEarth)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1)
                        .addGap(153, 153, 153)
                        .addComponent(jLabel3)
                        .addGap(50, 50, 50)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(scpCountries, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(btnAddSea, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnAddLand))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(20, 20, 20)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(89, 89, 89)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(btnSave)
                                    .addComponent(btnSubLand))
                                .addGap(75, 75, 75)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnSubSea, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnLoad, javax.swing.GroupLayout.Alignment.TRAILING)))))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(cbxCountries, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(lblName)
                            .addGap(13, 13, 13)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(20, 20, 20)
                            .addComponent(lblCodeRgb)
                            .addGap(15, 15, 15)
                            .addComponent(txtCodeRGB, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(20, 20, 20)
                            .addComponent(lblX, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtX, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(1, 1, 1)
                            .addComponent(lblY)
                            .addGap(4, 4, 4)
                            .addComponent(txtY, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(lblName))
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCodeRgb)
                            .addComponent(txtCodeRGB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblX)
                            .addComponent(txtX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblY)
                            .addComponent(txtY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel4))
                            .addComponent(cbxAir)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(cbxJoker)
                                    .addComponent(cbxCountries, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(cbxWater))
                            .addComponent(cbxEarth))
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel3))
                            .addComponent(jLabel5))
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(52, 52, 52)
                                        .addComponent(btnAddLand)
                                        .addGap(18, 18, 18)
                                        .addComponent(btnAddSea))
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(btnSubSea))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnSubLand)))
                                .addGap(42, 42, 42))
                            .addComponent(scpCountries, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(23, 23, 23))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnLoad)
                            .addComponent(btnSave))
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddSeaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddSeaActionPerformed
        Country countryToAdd = getCountryByName(lstUnasigned.getSelectedValue().toString());
        createSeaBound(current, countryToAdd);
        loadSea(current);
        loadUnasigned(current);
        lstUnasigned.requestFocusInWindow();
}//GEN-LAST:event_btnAddSeaActionPerformed

    private void btnSubSeaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubSeaActionPerformed
        Country countryToSub = getCountryByName(lstSeaBordering.getSelectedValue().toString());
        disolveSeaBound(current, countryToSub);
        loadSea(current);
        loadUnasigned(current);
        lstUnasigned.requestFocusInWindow();

}//GEN-LAST:event_btnSubSeaActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        Archive archive = new Archive("tjFiles\\World.wtj");
        archive.writeObject(world);
        cbxCountries.requestFocusInWindow();
        archivoSalida.writeString(salida, true);
        salida = "";
}//GEN-LAST:event_btnSaveActionPerformed

    private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
        Archive archive = new Archive("tjFiles\\World.wtj");
        Object object = archive.readObject();
        world = (World) object;
        Roster<Continent> continents = world.getContinents();
        rstCountries = world.getCountriesFromContinents(continents);
        fillComboBox(cbxCountries, rstCountries);
        cbxCountries.requestFocusInWindow();
}//GEN-LAST:event_btnLoadActionPerformed

    private void lstUnasignedValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstUnasignedValueChanged
        int index = lstUnasigned.getSelectedIndex();
        lstUnasigned.setSelectedIndex(index);

}//GEN-LAST:event_lstUnasignedValueChanged

    private void cbxCountriesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxCountriesActionPerformed
        current = getCountryByName(cbxCountries.getSelectedItem().toString());
        loadUnasigned(current);
        loadSea(current);
        loadLand(current);
        lstUnasigned.requestFocusInWindow();
    }//GEN-LAST:event_cbxCountriesActionPerformed

    private void btnAddLandActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddLandActionPerformed
        Country countryToAdd = getCountryByName(lstUnasigned.getSelectedValue().toString());
        createLandBound(countryToAdd, current);
        loadLand(current);
        loadUnasigned(current);
        lstUnasigned.requestFocusInWindow();
    }//GEN-LAST:event_btnAddLandActionPerformed

    private void btnSubLandActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubLandActionPerformed
        Country countryToSub = getCountryByName(lstLandBordering.getSelectedValue().toString());
        disolveLandBound(current, countryToSub);
        loadLand(current);
        loadUnasigned(current);
        lstUnasigned.requestFocusInWindow();
    }//GEN-LAST:event_btnSubLandActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Asign().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddLand;
    private javax.swing.JButton btnAddSea;
    private javax.swing.JButton btnLoad;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSubLand;
    private javax.swing.JButton btnSubSea;
    private javax.swing.JCheckBox cbxAir;
    private javax.swing.JComboBox cbxCountries;
    private javax.swing.JCheckBox cbxEarth;
    private javax.swing.JCheckBox cbxJoker;
    private javax.swing.JCheckBox cbxWater;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JLabel lblCodeRgb;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblX;
    private javax.swing.JLabel lblY;
    private javax.swing.JList lstLandBordering;
    private javax.swing.JList lstSeaBordering;
    private javax.swing.JList lstUnasigned;
    private javax.swing.JScrollPane scpCountries;
    private javax.swing.JTextField txtCodeRGB;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtX;
    private javax.swing.JTextField txtY;
    // End of variables declaration//GEN-END:variables
//    private Roster<Country> rstSeaCountries = new Roster<Country>();
//    private Roster<Country> rstLandCountries = new Roster<Country>();
    DefaultListModel lstmUnasigned = new DefaultListModel();
    DefaultListModel lstmSea = new DefaultListModel();
    DefaultListModel lstmLand = new DefaultListModel();
    DefaultComboBoxModel cbxmCountries = new DefaultComboBoxModel();
    private Roster<Country> rstUnasigned;
    private Roster<Country> rstCountries;
    Country current;
    World world;
    Archive archivoSalida = new Archive("tjFiles\\AsignacionDePaises.txt");
    String salida = "";

    private void fillList(JList lstCountry, Roster<Country> countries) {
        ((DefaultListModel) lstCountry.getModel()).clear();
        for (int i = 0; i < countries.count(); i++) {
            ((DefaultListModel) lstCountry.getModel()).addElement(countries.getElementInPosition(i).getName());
        }
    }

    private void fillComboBox(JComboBox cbxCountries, Roster<Country> rstCountries) {
        ((DefaultComboBoxModel) cbxCountries.getModel()).removeAllElements();
        for (int i = 0; i < rstCountries.count(); i++) {
            ((DefaultComboBoxModel) cbxCountries.getModel()).addElement(rstCountries.getElementInPosition(i).getName());
        }
    }

    private Country getCountryByName(String name) {
        Country country = null;
        for (int i = 0; i < rstCountries.count(); i++) {
            if (rstCountries.getElementInPosition(i).getName().compareToIgnoreCase(name) == 0) {
                country = rstCountries.getElementInPosition(i);
                break;
            }
        }
        return country;
    }

    private void loadFields(Country country) {
        clearFields();
        txtName.setText(country.getName());
        txtCodeRGB.setText(country.getColor().getCodeRGB());
        txtX.setText(country.getxPosition() + "");
        txtY.setText(country.getyPosition() + "");
        int[] types = country.getType().getTypeValues();
        for (int i = 0; i < types.length; i++) {
            if (types[i] == CardType.EARTH) {
                cbxEarth.setSelected(true);
            }
            if (types[i] == CardType.WATER) {
                cbxWater.setSelected(true);
            }
            if (types[i] == CardType.AIR) {
                cbxAir.setSelected(true);
            }
            if (types[i] == CardType.JOKER) {
                cbxJoker.setSelected(true);
            }
        }
//        rstSeaCountries = country.getSeaBorderingCountries();
//        rstLandCountries = country.getLandBorderingCountries();
        fillList(lstSeaBordering, country.getSeaBorderingCountries());
        fillList(lstLandBordering, country.getLandBorderingCountries());
    }

    private void updateCountry(Country country) {
        rstCountries.remove(getCountryByName(country.getName()));
        rstCountries.addInOrder(country);
        fillComboBox(cbxCountries, rstCountries);
    }

    private void clearFields() {

        cbxAir.setSelected(false);
        cbxEarth.setSelected(false);
        cbxJoker.setSelected(false);
        cbxWater.setSelected(false);
        lstmLand.clear();
        lstmSea.clear();
        lstmUnasigned.clear();
        txtCodeRGB.setText("");
        txtName.setText("");
        txtX.setText("");
        txtY.setText("");
//        rstSeaCountries = new Roster<Country>();
//        rstLandCountries = new Roster<Country>();
    }

    private void loadUnasigned(Country current) {
        rstUnasigned = cloneRoster(rstCountries);
        for (int i = 0; i < current.getSeaBorderingCountries().count(); i++) {
            rstUnasigned.remove(current.getSeaBorderingCountries().getElementInPosition(i));
        }
        for (int i = 0; i < current.getLandBorderingCountries().count(); i++) {
            rstUnasigned.remove(current.getLandBorderingCountries().getElementInPosition(i));
        }
        fillList(lstUnasigned, rstUnasigned);
    }

    private void loadSea(Country current) {
        fillList(lstSeaBordering, current.getSeaBorderingCountries());
    }

    private void loadLand(Country current) {
        fillList(lstLandBordering, current.getLandBorderingCountries());
    }

    public final Roster cloneRoster(Roster original) {
        Roster clon = new Roster();
        for (int i = 0; i < original.count(); i++) {
            clon.addLast(original.getElementInPosition(i));
        }
        return clon;
    }

    public void createLandBound(Country a, Country b) {
        if (a.getLandBorderingCountries().count() == 0) {
            a.getLandBorderingCountries().addInOrder(b);
            salida += a.getName() + ".getLandBorderingCountries().addInOrder(" + b.getName() + ")\n";
        } else {
            if (a.getLandBorderingCountries().indexOf(b) == -1) {
                a.getLandBorderingCountries().addInOrder(b);
                salida += a.getName() + ".getLandBorderingCountries().addInOrder(" + b.getName() + ")\n";
            }
        }
        if (b.getLandBorderingCountries().count() == 0) {
            b.getLandBorderingCountries().addInOrder(a);
            salida += b.getName() + ".getLandBorderingCountries().addInOrder(" + a.getName() + ")\n";

        } else {
            if (b.getLandBorderingCountries().indexOf(a) == -1) {
                b.getLandBorderingCountries().addInOrder(a);
                salida += b.getName() + ".getLandBorderingCountries().addInOrder(" + a.getName() + ")\n";
            }
        }
    }

    public void createSeaBound(Country a, Country b) {
        if (a.getSeaBorderingCountries().count() == 0) {
            a.getSeaBorderingCountries().addInOrder(b);
            salida += a.getName() + ".getSeaBorderingCountries().addInOrder(" + b.getName() + ")\n";
        } else {
            if (a.getSeaBorderingCountries().indexOf(b) == -1) {
                a.getSeaBorderingCountries().addInOrder(b);
                salida += a.getName() + ".getSeaBorderingCountries().addInOrder(" + b.getName() + ")\n";
            }
        }
        if (b.getSeaBorderingCountries().count() == 0) {
            b.getSeaBorderingCountries().addInOrder(a);
            salida += b.getName() + ".getSeaBorderingCountries().addInOrder(" + a.getName() + ")\n";

        } else {
            if (b.getSeaBorderingCountries().indexOf(a) == -1) {
                b.getSeaBorderingCountries().addInOrder(a);
                salida += b.getName() + ".getSeaBorderingCountries().addInOrder(" + a.getName() + ")\n";
            }
        }
    }

    public void disolveLandBound(Country a, Country b) {
        if (a.getLandBorderingCountries().count() != 0) {
            if (a.getLandBorderingCountries().indexOf(b) != -1) {
                a.getLandBorderingCountries().remove(b);
                salida += a.getName() + ".getLandBorderingCountries().remove(" + b.getName() + ")\n";
            }
        }

        if (b.getLandBorderingCountries().count() != 0) {
            if (b.getLandBorderingCountries().indexOf(a) != -1) {
                b.getLandBorderingCountries().remove(a);
                salida += b.getName() + ".getLandBorderingCountries().remove(" + a.getName() + ")\n";
            }
        }
    }

    public void disolveSeaBound(Country a, Country b) {
        if (a.getSeaBorderingCountries().count() != 0) {
            if (a.getSeaBorderingCountries().indexOf(b) != -1) {
                a.getSeaBorderingCountries().remove(b);
                salida += a.getName() + ".getSeaBorderingCountries().remove(" + b.getName() + ")\n";
            }
        }

        if (b.getSeaBorderingCountries().count() != 0) {
            if (b.getSeaBorderingCountries().indexOf(a) != -1) {
                b.getSeaBorderingCountries().remove(a);
                salida += b.getName() + ".getSeaBorderingCountries().remove(" + a.getName() + ")\n";
            }
        }
    }
}
